<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Admin_kontak extends CI_Controller {

	function __construct() {
		parent::__construct();

		//jika digunakan di mayoritas function, panggil disini
		//model
		$this->load->model('m_kontak');

		//library breadcrum/untuk navigasi
		$this->load->library('breadcrumb');
	}

	function index() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//Layout
		//title
		$this->layout->set_title('Data Kontak');

		//meta description jika perlu
		$this->layout->set_desc('Data Kontak');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('About', site_url('admin_kontak#'));
		$this->breadcrumb->add_crumb('Kontak');
		

		//paging dan ambil data dari model
		$this->load->library('paging');                         		//Load library "paging"
		$config['sql']      				= $this->m_kontak->get();				//Query SQL - bukan array lho ya...
		$config['per_page'] 				= 15;                            //jumlah item tiap halaman
		$data = $this->paging->create_pagination($config);   				//Load fungsi di library "paging" untuk membuat pagination. 

		//judul besar
		$data['primary_title'] 			= 'About';
		$data['sub_primary_title'] 	= '';

		//menghilangka session jika ada
		$this->session->unset_userdata('keyword');

		//menggunakan theme back
		$this->layout->theme('back','admin_kontak/index', $data);
	}

	function edit($id_kontak = '') {

		//rule type pada function ini: Update
		$this->rule->type('U');

		//layout
		//title
		$this->layout->set_title('Edit Data KOntak');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('About', site_url('admin_kontak/edit#'));
		$this->breadcrumb->add_crumb('Edit Kontak');

		//judul besar
		$data['primary_title'] 			= 'About';
		$data['sub_primary_title'] 	= '';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] = $this->m_kontak->get_by($id_kontak);

		//menggunakan theme back
		$this->layout->theme('back','admin_kontak/edit', $data);
	}

	function update() {

		//rule type pada function ini: Update
		$this->rule->type('U');
		if (isset($_POST) && !empty($_POST)) {

			$id_kontak = $this->input->post('id_kontak');

			$dataUpdate = array(				 
				'alamat' => $this->input->post('alamat'), 
				'phone' => $this->input->post('phone'), 
				'email' => $this->input->post('email')
			);

			if(!$this->m_kontak->update($id_kontak, $dataUpdate)){
				$this->session->set_flashdata('gagal', 'Data Kontak gagal diperbaharui.');
				redirect('admin_kontak','refresh');
			}else{
				$this->session->set_flashdata('sukses', 'Data Kontak berhasil diperbaharui.');
				redirect('admin_kontak','refresh');
			}

		}else{
			show_404();
		}
	}

}